<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2023/7/6
 * Time: 10:57
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Star extends Base
{
    /*
     * 服务评价项目星级列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 100;
            $count = Db::table('up_star')->count();// 获取总条数
            $lists = Db::table('up_star')
                ->field('star_id, star_name, star_status, star_time')
                ->order('star_time desc')
                ->page($Nowpage, $limits)
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }

        return $this->fetch();
    }

    /*
     * 添加项目
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function addStar(){
        if(Request::instance()->isPost()){
            $star_name = input('star_name');

            $has = Db::table('up_star')->where('star_name', $star_name)->find();
            if($has) return json(['code' => 100, 'msg' => '该项目已存在']);

            $insert = [
                'star_name' => $star_name,
                'star_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_star')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }

    /*
     * 编辑项目
     * @return mixed|\think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function updateStar(){
        $star_id = input('star_id');
        if(Request::instance()->isPost()){
            $star_name = input('star_name');
            $star_status = input('star_status');

            $has = Db::table('up_star')->where('star_name', $star_name)->find();
            if($has && $has['star_id'] != $star_id) return json(['code' => 100, 'msg' => '该项目已存在']);

            $update = [
                'star_id' => $star_id,
                'star_name' => $star_name,
                'star_status' => $star_status,
                'star_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_star')->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $star = Db::table('up_star')->where('star_id', $star_id)->find();
        return $this->fetch('', ['star' => $star]);
    }

    /*
     * 启用/禁用
     * @return \think\response\Json
     */
    public function setStatus(){
        $star_id = input('id');
        $status = Db::table('up_star')->where('star_id', $star_id)->value('star_status');
        if($status == 1){
            $newStatus = 2;
        }else{
            $newStatus = 1;
        }
        $res = Db::table('up_star')->where('star_id', $star_id)->setField('star_status', $newStatus);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /*
     * 删除项目
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function delStar(){
        $star_id = input('id');
        $res = Db::table('up_star')->delete($star_id);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}